package com.lzs.Controller;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.lzs.PO.Certificate;
import com.lzs.PO.CertificateExcel;
import com.lzs.PO.Member;
import com.lzs.PO.Page;
import com.lzs.Service.CertificateService;
import com.lzs.Service.MemberService;
import com.lzs.util.Msg;

@Controller
@RequestMapping(value = "/certificate")
public class CertificateController {
	@Autowired
	MemberService memberService;
	@Autowired
	CertificateService certificateService;
	
	@RequestMapping(value = "/certificate_member_list")
	public String certificateMemberList(Model model, HttpServletRequest request, @RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "5") Integer pageSize, String keyword) throws Exception {

		Page<Member> page = this.memberService.findMemberPagelist(keyword, pageSize, pageNum);
		System.out.println("!!!!!!!!!!!!!!!");
		model.addAttribute("page", page);

		return "/certificate/certificate_member_list";
	}

	@RequestMapping(value = "/certificate_list")
	public String certificateList(String rural_id,Model model,
			                                     @RequestParam(defaultValue = "1") Integer pageNum,
			                                     @RequestParam(defaultValue = "5") Integer pageSize, 
			                                      String keyword) {
		
		Page<Certificate> page = this.certificateService.findCertificatePagelist(rural_id,keyword, pageSize, pageNum);
		
		model.addAttribute("page", page);
		
		return"/certificate/certificate_list";
	}
	
	@ResponseBody
	@RequestMapping(value = "certificate_check")
	public Msg checkCertificate(String disease_name,String rural_id) {
		
		boolean b = this.certificateService.countCertificateByDisease(disease_name,rural_id);
		
		System.out.println(b);
		
		if(b) {
			
			return Msg.success();
			
		}else {
			
			return Msg.fail();
			
		}
	}
	
	@RequestMapping(value = "/certificate_add")
	public String addCertificate(Model model,String rural_id) {
		
		Member member = this.certificateService.findCertificateWithMemberByRuralId(rural_id);
		
		model.addAttribute("member", member);
		
		return"/certificate/certificate_add";
	}
	
	@RequestMapping(value = "/certificate_add_action")
	@ResponseBody
	public Msg addCertificateAction(String member_name,String rural_id,String disease_name,String start_time,String end_time) {
		
		try {
			
			//获取系统当前时间

			SimpleDateFormat currentTime = new SimpleDateFormat("yyyyMMddHHmmss");
			
			String certificate_time = currentTime.format(new java.util.Date());
			
			String certificate_id = "C"+certificate_time;
			
			Map<String,Object> map = new HashMap<String,Object>();
			
			map.putIfAbsent("certificate_id", certificate_id);
			
			map.put("member_name",member_name);
			
			map.put("rural_id",rural_id);
			
			map.put("disease_name",disease_name);
			
			map.put("start_time", start_time);
			
			map.put("end_time",end_time);
			
			this.certificateService.addCertificate(map);
		
			return Msg.success();
			
		}catch (Exception e) {
			
			e.printStackTrace();
			
			return Msg.fail();
			
		}
	}
	
	@RequestMapping(value = "/certificate_update")
	public String updateCertificate(String certificate_id,String rural_id,Model model) {
		
		Certificate certificate = this.certificateService.findCertificateWithMemberByCertificateIdAndRuralId(certificate_id,rural_id);
		
		model.addAttribute("certificate", certificate);
		
		return"/certificate/certificate_update";
	}
	
	@ResponseBody
	@RequestMapping(value = "certificate_update_action")
	public Msg updateCertificateAction(String certificate_id, String cetificate_id,String rural_id,String disease_name,String start_time,String end_time) {
		
			try {
			
			
			
			Map<String,Object> map = new HashMap<String,Object>();
			
			map.putIfAbsent("certificate_id", certificate_id);
			
			map.put("rural_id",rural_id);
			
			map.put("disease_name",disease_name);
			
			map.put("start_time", start_time);
			
			map.put("end_time",end_time);
			
			this.certificateService.updateCertificate(map);
		
			return Msg.success();
			
		}catch (Exception e) {
			
			e.printStackTrace();
			
			return Msg.fail();
			
		}
	}
	
	@RequestMapping(value = "certificate_delete")
	@ResponseBody
	public Msg deleteCertificate(String certificate_id) {
		
		try {
			
			this.certificateService.deleteCertificate(certificate_id);
			
			return Msg.success();
			
		}catch(Exception e) {
			
			e.printStackTrace();
			
			return Msg.fail();
			
		}
	}
	@RequestMapping(value = "/certificate_list_all")
	public String certificateList4All(Model model, HttpServletRequest request, @RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "5") Integer pageSize, String keyword) throws Exception {

		Page<Certificate> page = this.certificateService.findCertificatePagelist4All(keyword, pageSize, pageNum);
	
		model.addAttribute("page", page);
		
		model.addAttribute("keyword", keyword);

		return "/certificate/certificate_list_all";
	}
	
	@RequestMapping(value = "/certificate_inputExcel")
	@ResponseBody
	public Msg inputCertificate4Excel(String keyword) {
		
		List<CertificateExcel> excelModuleList = this.certificateService.findCertificate4InputExcel(keyword);

        System.out.println(excelModuleList);
        System.out.println(keyword);
        
        OutputStream out = null;

        try{

            out = new FileOutputStream("D:\\mxbbx_Excel\\Certificate.xls");

            ExcelWriter writer = EasyExcelFactory.getWriter(out);

            Sheet sheet1 = new Sheet(1, 0, CertificateExcel.class);

            sheet1.setSheetName("请假信息列表");


            writer.write(excelModuleList, sheet1);


            // 将上下文中的最终 outputStream 写入到指定文件中
            writer.finish();

            return Msg.success();
        }catch (FileNotFoundException e) {

            e.printStackTrace();

            return Msg.fail();

        }finally {
            try {
                // 关闭流
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
	}
}
